package com.moive.news.tqnewsapplication.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.adapter.FragmentVpAdapter;
import com.moive.news.tqnewsapplication.base.BaseFragment;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/7.
 */
public class HomeFragment extends BaseFragment {
    private static final String TAG = "HomeFragment";
    @BindView(R.id.home_tab)
    TabLayout homeTab;
    @BindView(R.id.vp)
    ViewPager vp;
    Unbinder unbinder;
    private List<String> tablist = new ArrayList<>();
    private ArrayList<Fragment> fgs = new ArrayList<Fragment>();
    private FragmentVpAdapter fragmentVpAdapter;
    private Context context;
    private View homeView;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        homeView = inflater.inflate(R.layout.fragment_home, container, false);
        homeView.setOnClickListener(null);
        unbinder = ButterKnife.bind(this, homeView);
        init(homeView);
        return homeView;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    private void init(View view) {
        tablist.clear();
        tablist.add("星闻");
        tablist.add("小道");
        tablist.add("糗事");
        fgs.add(new NewsListFragment());
        fgs.add(new XiaodaoFragment());
        fgs.add(new QiuShiFragment());
        fragmentVpAdapter = new FragmentVpAdapter(getChildFragmentManager(), tablist, fgs);
        vp.setAdapter(fragmentVpAdapter);
        homeTab.setupWithViewPager(vp, true);
        vp.setOffscreenPageLimit(2);
    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden){
            LogUtil.i("lidaming","onHiddenChanged");
            JCVideoPlayer.releaseAllVideos();
        }
    }
}